package com.wu.springbootmqproducer.Config;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * 消息确认机制
 */
@Component
public class MyConfirmListener  implements RabbitTemplate.ConfirmCallback {


    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    //监听器设置进rabbitmq
    @PostConstruct
    public void init(){
        rabbitTemplate.setConfirmCallback(this);
    }


    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String s) {
        //参数b 表示消息确认结果
        //参数s 表示发送的消息
        if(ack){
            System.out.println("消息发送到交换机成功！");
        }else{
            System.out.println("消息发送到交换机失败！");
            amqpTemplate.convertAndSend("ex6","k1",s);
        }
    }
}
